﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace odinshrine
{
    enum state
    {
        enChasse,
        patrouille,
        tire
    }

    class GestionEnnemi
    {
        Texture2D barreVie;

        public LinkedList<BadGuys> tabEnnemi1 = new LinkedList<BadGuys>();

        public void EnnemisIA(Joueur player, GameTime gameTime, map Map, GestionPhysics phys)
        {
            foreach (BadGuys badguys in tabEnnemi1)
            {
                badguys.IA(player, gameTime, Map, phys);
            }
        }

        public void AddEnnemi(BadGuys badguys)
        {
            tabEnnemi1.AddLast(badguys);
        }

        public void LoadEnnemi(ContentManager Content, GestionPhysics phys)
        {
            /*BadGuys Type1 = new BadGuys(Content.Load<Texture2D>("images/ennemi"), 1, 1, 50, 3, Armerie.Deagle, 10);
            Type1.finalAngle = 0;*/
            barreVie = Content.Load<Texture2D>("vie");
            //Type1.ballesListe.Init();
        }

        public void DrawEnnemi(SpriteBatch spriteBatch, GraphicsDeviceManager graphics)
        {
            foreach (BadGuys ennemi in tabEnnemi1)
            {
                spriteBatch.Draw(ennemi.texture,
                    new Rectangle((int)ennemi.Position.X, (int)ennemi.Position.Y,
                        (int)ennemi.scale * ennemi.width, (int)ennemi.scale * ennemi.height), Color.White);
                spriteBatch.Draw(barreVie,
                    new Rectangle((int)ennemi.Position.X + 11, (int)ennemi.Position.Y - 10, ennemi.Vie, barreVie.Height), Color.Orange);
                ennemi.ballesListe.Draw(spriteBatch, ennemi.arme.textureballe.texture, graphics);
            }
        }

        private void TirEnnemi(GameTime gameTime)
        {
            foreach (BadGuys ennemi in tabEnnemi1)
            {
                if (ennemi.timer_tirs.Milliseconds >= ennemi.arme.cadence)
                {
                    ennemi.Tir();
                    ennemi.timer_tirs = TimeSpan.Zero;
                }
            }
        }
    }

    class Sentinelle : BadGuys
    {
        int temps = 0;

        public Sentinelle(ContentManager Content)
            : base(Content.Load<Texture2D>("images/ennemi"), 1, 1, 50, 3, Armerie.Deagle, 15)
        {

        }
    
        public override void IA(Joueur player, GameTime gameTime, map Map, GestionPhysics physEngine)
        {
            temps += 1;
            //Il faut régler avec la taille des textures aussi + Collision map
            switch (Ennemistate)
            {
                case state.patrouille:

                    if (temps <= 150)
                        Position.X += 1;

                    if (temps > 150 && temps < 300)
                        Position.X -= 1;

                    if (temps >= 300)
                        temps = 0;

                    break;

                case state.enChasse:
                    if (player.Position.X > Position.X)
                    {
                        Position.X += Vitesse;
                    }
                    if (player.Position.X < Position.X)
                    {
                        Position.X -= Vitesse;
                        sauter = true;
                    }
                    break;

                case state.tire:
                    if (player.Position.X > Position.X)
                    {
                        //TIR
                        if (timer_tirs.Milliseconds >= arme.cadence)
                        {
                            ballesListe.Add(new Vector2(Position.X, Position.Y - 4 + (height / 2)),ballesListe.tableau);
                            directionBalles.Add(new Vector2(1, 0), ballesListe.tableau);
                            timer_tirs = TimeSpan.Zero;
                        }
                    }
                    if (player.Position.X < Position.X)
                    {
                        //TIR
                        if (timer_tirs.Milliseconds >= arme.cadence)
                        {
                            ballesListe.Add(new Vector2(Position.X, Position.Y - 4 + (height / 2)), ballesListe.tableau);
                            directionBalles.Add(new Vector2(-1, 0), ballesListe.tableau);
                            timer_tirs = TimeSpan.Zero;
                        }
                    }
                    break;

            }

            MouvementBalles(gameTime);
            timer_tirs += gameTime.ElapsedGameTime;
            for (int k = 0; k < ballesListe.tableau.GetLength(0); k++)
            {
                if (ballesListe.tableau[k, 1] != -1)
                {
                    if (Collisions.Rectangles(new Rectangle((int)player.Position.X, (int)player.Position.Y, (int)player.width, (int)player.height), new Rectangle((int)ballesListe.tableau[k, 0], (int)ballesListe.tableau[k, 1], (int)arme.textureballe.width, (int)arme.textureballe.height)))
                    {
                        if (Collisions.Pixel(Collisions.TextureColor(player), Collisions.TextureColor(arme.textureballe), player.Position, new Vector2(ballesListe.tableau[k, 0], ballesListe.tableau[k, 1])))
                        {
                            player.Vie--;
                            ballesListe.tableau[k, 0] = -1;
                            ballesListe.tableau[k, 1] = -1;
                            directionBalles.tableau[k, 0] = -1;
                            directionBalles.tableau[k, 1] = -1;
                        }
                    }
                }
            }
        

            AngleMe(gameTime);
            int distance = Math.Abs((int)player.Position.X + player.width / 2 - (int)(Position.X + width / 2));
            if (distance > 300)
            {
                Ennemistate = state.patrouille;
            }
            else if (distance > 150)
            {
                Ennemistate = state.enChasse;
            }
            else
            {
                Ennemistate = state.tire;
            }
        }                
    }   
}

  
           
     
                
            

  

